package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentExample;
import com.dongdongshop.service.ContentService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@DubboService
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<TbContent> listContents() {

        return tbContentMapper.selectByExample(null);
    }

    @Override
    public void addContent(TbContent tbContent) {
        tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public TbContent toUpdate(Long id) {
        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateContent(TbContent tbContent) {
        tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    public void delContent(List<Long> ids) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tbContentMapper.deleteByExample(example);
    }

    @Override
    public List<TbContent> listContentsByCateId(Long cateId) {

        ValueOperations str = stringRedisTemplate.opsForValue();


        if (cateId == 1){//---------------轮播图

            String Content1 = (String) str.get("Content1");
            if (Content1 != null){
                return new Gson().fromJson(
                        Content1,new TypeToken<List<Map<String, Object>>>(){}.getType());
            }else{
                List<TbContent> tbContents = tbContentMapper.selectSlideshow(cateId);
                String map = new Gson().toJson(tbContents);
                str.set("Content1",map);
                return tbContents;
            }
        }else if(cateId==2){//-------------今日推荐
            String Content2 = (String) str.get("Content2");
            if (Content2 != null){
                return new Gson().fromJson(
                        Content2,new TypeToken<List<Map<String, Object>>>(){}.getType());
            }else{
                List<TbContent> tbContents = tbContentMapper.selectSlideshow(cateId);
                String map = new Gson().toJson(tbContents);
                str.set("Content2",map);
                return tbContents;
            }

        }else if (cateId == 4){//-------------猜你喜欢
            SetOperations set = stringRedisTemplate.opsForSet();
            if (stringRedisTemplate.hasKey("Content4")){
                //存在
                Set<String> content4 = set.distinctRandomMembers("Content4", 6L);

                List<TbContent> collect = content4.stream().map(l -> JSON.parseObject(l, TbContent.class)).collect(Collectors.toList());
                return collect;
            }else{
                //不存在
                List<TbContent> tbContents = tbContentMapper.selectSlideshow(cateId);
                for (TbContent tbContent : tbContents) {
                    set.add("Content4", JSON.toJSONString(tbContent));
                }
                Set<String> content4 = set.distinctRandomMembers("Content4", 6L);

                List<TbContent> collect = content4.stream().map(l -> JSON.parseObject(l, TbContent.class)).collect(Collectors.toList());
                return collect;
            }
        }
        return null;

    }
}
